#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e6+5,mod=1e9+7;
int n,a[maxn],b[maxn],c[maxn],s[2][maxn],fa[maxn],ans[maxn],p;
int read(){
    int s=0;
    char ch=getchar(),last=' ';
    while(ch<'0'||ch>'9')
        last=ch,ch=getchar();
    while(ch>='0'&&ch<='9')
        s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
    return last=='-'?-s:s;
}
int power(int a,int b){
    int res=1;
    while(b){
        if(b&1)
            res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
void rotate(int x){
    int y=fa[x],z=fa[y];
    int a=(x==s[1][y]),b=(y==s[1][z]);
	if(z)
    	s[b][z]=x;
	fa[x]=z;
    fa[y]=x;
    s[a][y]=s[a^1][x];
    if(s[a^1][x])
    	fa[s[a^1][x]]=y;
    s[a^1][x]=y;
}
void splay(int x){
    while(fa[x]){
        int y=fa[x],z=fa[y];
        if(z)
        	((s[0][y]==x)^(s[0][z]==y))?rotate(x):rotate(y);
        if(fa[x])
            rotate(x);
    }
}
void dfs(int u,int d){
    if(!u)
        return;
    ans[u]=(ans[u]+d*p%mod)%mod;
    dfs(s[0][u],d+1);
    dfs(s[1][u],d+1);
}
signed main(){
    freopen("dream.in","r",stdin);
    freopen("dream.out","w",stdout);
    n=read();
    p=power(n,mod-2);
    for(int i=1;i<=n;i++){
        a[i]=read(),b[i]=read();
        if(a[i])
            c[a[i]]=i;
        if(b[i])
            c[b[i]]=i;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            s[0][j]=a[j],s[1][j]=b[j],fa[j]=c[j];
        splay(i);
        dfs(i,0);
    }
    for(int i=1;i<=n;i++)
        printf("%lld\n",ans[i]*n%mod);
    return 0;
}
